package DataStructure.LinkedLearning.SingleLinkedList.ReverseLinked;

/**
 *  反转链表 --- 原地暴力反转
 */
public class Demo2 {
    public static void main(String[] args) {
        //Node node7 = new Node("7", null);
        //Node node6 = new Node("6", node7);
        //Node node5 = new Node("5", node6);
        //Node node4 = new Node("4", node5);
        //Node node3 = new Node("3", node4);
        Node node2 = new Node("2", null);
        Node node1 = new Node("1", node2);

        System.out.println("反转前：");
        System.out.println(node1);
        Node head = reverse(node1);
        System.out.println("反转后：");
        System.out.println(head);
    }


    private static Node reverse(Node head) {
        if(head.next == null) {
            return head;
        }
        Node left = null;
        Node mid = null;
        Node right = null;
        if(head.next.next == null) {
            left = head;
            right = head.next;
            right.next = left;
            left.next = null;
            return right;
        }
        left = head;
        right = head.next;
        head.next = null;
        while(right.next != null) {
            mid = right;
            right = right.next;
            mid.next = left;
            left = mid;
        }
        right.next = mid;
        return right;

    }
}


